﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework.DataTable;
using GameFramework.Event;
using UnityEngine.SceneManagement;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace VRLauncher
{
    public class ProcedureChangeScene : ProcedureBase
    {
        private int m_BackgroundMusicId = 1;
        //菜单场景ID
        private const int MENU_SCENE_ID = 1;
        //是否是菜单场景
        private bool changeToMian = false;
        private bool isChangeSceneComplete = false;
        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);

            isChangeSceneComplete = false;

            GameManager.Event.Subscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess);
            GameManager.Event.Subscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
            GameManager.Event.Subscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);

            // 停止所有声音
            GameManager.Sound.StopAllLoadingSounds();
            GameManager.Sound.StopAllLoadedSounds();

            // 隐藏所有实体
            GameManager.Entity.HideAllLoadingEntities();
            GameManager.Entity.HideAllLoadedEntities();

            //关闭所有界面
            GameManager.UI.CloseAllLoadedUIForms();
            GameManager.UI.CloseAllLoadingUIForms();

            // 卸载所有场景
            string[] loadedSceneAssetNames = GameManager.Scene.GetLoadedSceneAssetNames();
            for (int i = 0; i < loadedSceneAssetNames.Length; i++)
            {
                GameManager.Scene.UnloadScene(loadedSceneAssetNames[i]);
            }
            // 还原游戏速度
            GameManager.Base.ResetNormalGameSpeed();

            //还记得ProcedurePreload.cs中对应的设置吗？这里拿到的就是之前设置的值。
            int sceneId = procedureOwner.GetData<VarInt32>("NextSceneId");
            changeToMian = sceneId == MENU_SCENE_ID;
            //如果你还没有处理数据表，记得使用菜单栏的工具生成这些DRXXX.cs。
            IDataTable<DRScene> dtScene = GameManager.DataTable.GetDataTable<DRScene>();
            DRScene drScene = dtScene.GetDataRow(sceneId);
            if (drScene == null)
            {
                Log.Warning("Can not load scene '{0}' from data table.", sceneId.ToString());
                return;
            }
            //场景组件加载场景
            GameManager.Scene.LoadScene(AssetUtility.GetSceneAsset(drScene.AssetName), null, LoadSceneMode.Additive, false, Constant.AssetPriority.SceneAsset, false, "", this);
            m_BackgroundMusicId = drScene.BackgroundMusicId;
         
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            GameManager.Event.Unsubscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess);
            GameManager.Event.Unsubscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
            GameManager.Event.Unsubscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);

      
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (!isChangeSceneComplete)
            {
                return;
            }

            if (changeToMian)
            {
                //切换到主流程
                ChangeState<ProcedureMain>(procedureOwner);
            }
            else
            {
                //切换到房间流程
                ChangeState<ProcedureHouse>(procedureOwner);
            }

        }

        private void OnLoadSceneSuccess(object sender, GameEventArgs e)
        {
            Log.Info("OnLoadSceneSuccess");
            LoadSceneSuccessEventArgs ne = (LoadSceneSuccessEventArgs)e;
            if (ne.UserData != this)
            {
                Log.Info("ne.UserData != this");
                return;
            }
            Log.Info("Load scene '{0}' OK.", ne.SceneAssetName);
            if (m_BackgroundMusicId > 0)
            {
                GameManager.Sound.PlayMusic(m_BackgroundMusicId);
            }
            isChangeSceneComplete = true;
        }

        private void OnLoadSceneFailure(object sender, GameEventArgs e)
        {
            LoadSceneFailureEventArgs ne = (LoadSceneFailureEventArgs)e;
            if (ne.UserData != this)
            {
                return;
            }

            Log.Error("Load scene '{0}' failure, error message '{1}'.", ne.SceneAssetName, ne.ErrorMessage);
        }

        private void OnLoadSceneUpdate(object sender, GameEventArgs e)
        {
            LoadSceneUpdateEventArgs ne = (LoadSceneUpdateEventArgs)e;
            if (ne.UserData != this)
            {
                return;
            }

            Log.Info("Load scene '{0}' update, progress '{1}'.", ne.SceneAssetName, ne.Progress.ToString("P2"));
        }

       
    }
}
